package com.star.es.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;

import lombok.Data;

/**
 * 查询参数封装
 * @author Administrator
 */
@Data
@SuppressWarnings("serial")
public class ESQuery implements Serializable{
	
	private List<String> indexs; /// 索引
	private List<String> types; // 类型
	private SearchType searchType; // 搜索类型 默认：DFS_QUERY_THEN_FETCH
	private QueryBuilder query; // 查询参数
	private QueryBuilder filter; // 过滤参数
	private List<AggregationBuilder> aggregations; // 聚合条件（类似于切面查询）
	private String sortField; // 排序字段
	private SortOrder order; // 排序方式
	private int from=0; // 页码
	private int size=10; // 每页显示的数量
	private int terminate; // 在击中多少个doc后返回结果
	private int scroll=60000;
	private long timeout=10000l; // 搜索超时时间/单位秒
	private float minScore; // 设置Score的最小数量
	
	@Data
	public static class ESQueryBuilder implements Serializable{
		private ESQuery esQuery;
		public ESQueryBuilder() {
			super();
			esQuery=new ESQuery();
			esQuery.indexs=new ArrayList<>();
			esQuery.types=new ArrayList<>();
			esQuery.query=QueryBuilders.matchAllQuery(); // 默认查询所有
			esQuery.aggregations=new ArrayList<>(); // 聚合条件
			esQuery.searchType=SearchType.DFS_QUERY_THEN_FETCH;
			esQuery.sortField=FieldSortBuilder.DOC_FIELD_NAME;
			esQuery.order=SortOrder.ASC;
		}
		public ESQueryBuilder(ESQuery esQuery){
			super();
			this.esQuery=esQuery;
		}
		public ESQuery build(){
			return esQuery;
		}
		public ESQueryBuilder scroll(int scroll){
			esQuery.scroll=scroll;
			return this;
		}
		public ESQueryBuilder terminate(int terminate){
			esQuery.terminate=terminate;
			return this;
		}
		public ESQueryBuilder size(int size){
			esQuery.size=size;
			return this;
		}
		public ESQueryBuilder from(int from){
			esQuery.from=from;
			return this;
		}
		public ESQueryBuilder order(SortOrder order){
			esQuery.order=order;
			return this;
		}
		public ESQueryBuilder sortField(String sortField){
			esQuery.sortField=sortField;
			return this;
		}
		
		public ESQueryBuilder index(String index){
			esQuery.indexs.add(index);
			return this;
		} 
		
		public ESQueryBuilder index(String...indexs){
			esQuery.indexs.addAll(Arrays.asList(indexs));
			return this;
		} 
		
		public ESQueryBuilder type(String type){
			esQuery.types.add(type);
			return this;
		} 
		
		public ESQueryBuilder type(String...types){
			esQuery.types.addAll(Arrays.asList(types));
			return this;
		} 
		public ESQueryBuilder type(List<String> types){
			esQuery.types.addAll(types);
			return this;
		} 
		
		public ESQueryBuilder searchType(SearchType searchType){
			this.esQuery.setSearchType(searchType);
			return this;
		} 
		public ESQueryBuilder aggregations(List<AggregationBuilder> aggregationBuilders){
			esQuery.aggregations.addAll(aggregationBuilders);
			return this;
		} 
	}

}




























